//
//  UIPopView.m
//  UISliderAnimation
//
//  Created by ShiMac on 15/5/24.
//  Copyright (c) 2015年 guoyan. All rights reserved.
//

#import "UIPopView.h"
#define FlagHeight 13



@interface UIPopView ()
{
    CAShapeLayer *backGroundLayer_;
    CATextLayer  *textLayer_;
    NSMutableAttributedString *_attributedString;
}
@end



@implementation UIPopView


-(id)initWithFrame:(CGRect)frame
{
    
    if (self=[super initWithFrame:frame]) {
        
        self.layer.anchorPoint=CGPointMake(0.5, 1);
        
        //背景
        backGroundLayer_=[CAShapeLayer layer];
        
        //文字
        textLayer_=[CATextLayer layer];
        textLayer_.alignmentMode=kCAAlignmentCenter;
        textLayer_.contentsScale=[[UIScreen mainScreen] scale];
        _attributedString = [[NSMutableAttributedString alloc] initWithString:@" " attributes:nil];
        
        
        [self.layer addSublayer:backGroundLayer_];
        [self.layer addSublayer:textLayer_];
        
        
    }
    return self;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
    
    textLayer_.frame=CGRectMake(self.bounds.origin.x, (self.bounds.size.height-FlagHeight-[textLayer_.string size].height)/2, self.bounds.size.width, [textLayer_.string size].height);
    
    
    backGroundLayer_.frame=self.bounds;
    
    
    CGRect rect=self.bounds;
    rect.size.height=rect.size.height-FlagHeight;
    UIBezierPath *path=[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:4];
    
    UIBezierPath *flagPath=[UIBezierPath bezierPath];
    [flagPath moveToPoint:CGPointMake((rect.size.width-FlagHeight)/2, rect.size.height)];
    [flagPath addLineToPoint:CGPointMake(rect.size.width/2, rect.size.height+FlagHeight)];
    [flagPath addLineToPoint:CGPointMake((rect.size.width+FlagHeight)/2, rect.size.height)];
    [flagPath closePath];
    [path appendPath:flagPath];
    
    
    backGroundLayer_.path=path.CGPath;
    
}


-(void)setString:(NSString *)string{
    
    [[_attributedString mutableString] setString:string];
    
    [_attributedString addAttribute:NSForegroundColorAttributeName
                              value:(id)[UIColor blackColor].CGColor
                              range:NSMakeRange(0, [_attributedString length])];
    [_attributedString addAttribute:NSFontAttributeName
                              value:[UIFont systemFontOfSize:13]
                              range:NSMakeRange(0, [_attributedString length])];
    
    textLayer_.string=_attributedString;
}

-(void)setColor:(UIColor *)color
{
    
    backGroundLayer_.fillColor=color.CGColor;
    backGroundLayer_.strokeColor=color.CGColor;
    
    
}

-(void)showAnimation
{
    
    [CATransaction begin];
    
    CABasicAnimation *basic=[CABasicAnimation animationWithKeyPath:@"transform"];
    basic.fromValue=[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)];
    basic.toValue=[NSValue valueWithCATransform3D:CATransform3DIdentity];
    basic.fillMode=kCAFillModeForwards;
    basic.removedOnCompletion=NO;
    basic.timingFunction=[CAMediaTimingFunction functionWithControlPoints:0.8 :2.5 :0.35 :0.5];
    
    basic.duration = 0.6;
    [self.layer addAnimation:basic forKey:@"transform"];
    
    CABasicAnimation* fadeInAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
    fadeInAnim.fromValue = @0;
    fadeInAnim.duration = 0.1;
    fadeInAnim.toValue = @1.0;
    [self.layer addAnimation:fadeInAnim forKey:@"opacity"];
    
    self.layer.opacity = 1.0;
    
    [CATransaction commit];
    
    
}

-(void)hideAnimation{
    
    [CATransaction begin]; {
        CABasicAnimation *scaleAnim = [CABasicAnimation animationWithKeyPath:@"transform"];
        scaleAnim.fromValue = [self.layer.presentationLayer valueForKey:@"transform"];
        scaleAnim.toValue = [NSValue valueWithCATransform3D:CATransform3DMakeScale(0.5, 0.5, 1)];
        scaleAnim.duration = 0.6;
        scaleAnim.removedOnCompletion = NO;
        scaleAnim.fillMode = kCAFillModeForwards;
        [scaleAnim setTimingFunction:[CAMediaTimingFunction functionWithControlPoints:0.1 :-2:0.3 :3]];
        [self.layer addAnimation:scaleAnim forKey:@"transform"];
        
        CABasicAnimation* fadeOutAnim = [CABasicAnimation animationWithKeyPath:@"opacity"];
        fadeOutAnim.fromValue = [self.layer.presentationLayer valueForKey:@"opacity"];
        fadeOutAnim.toValue = @0.0;
        fadeOutAnim.duration = 0.8;
        [self.layer addAnimation:fadeOutAnim forKey:@"opacity"];
        self.layer.opacity = 0.0;
    } [CATransaction commit];
    
}


@end
